package com.qhit.controller;

import com.qhit.pojo.Goods;
import com.qhit.pojo.Order;
import com.qhit.pojo.Tourist;
import com.qhit.service.GoodsService;
import com.qhit.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: lyb
 * @description: ll
 * @author: DYF
 * @create: 2020-10-22 09:33
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/list")
    public String list(Model m , String keywords
            , @RequestParam(defaultValue = "1",required = false) Integer page
            , @RequestParam(defaultValue = "2",required = false) Integer pageSize){
        HashMap hashMap = new HashMap();
        hashMap.put("keywords",keywords);

        hashMap.put("page",page);
        hashMap.put("start",(page-1)*pageSize);
        hashMap.put("pageSize",pageSize);

        List<Map> order= orderService.getList(hashMap);
        long total= orderService.getListCount(hashMap);

        hashMap.put("totalPage",(total%pageSize==0)?(total/pageSize):(total/pageSize+1));

        m.addAttribute("orderList",order);
        m.addAttribute("myParams",hashMap);
        return "order/list";
    }


    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        model.addAttribute("goodss",goodsService.getList(null));
        return "order/add";
    }

    @RequestMapping(value = "/add",method= RequestMethod.POST)
    public String add(Order order, Model m){
        order.setOrderDate(new Date());
        order.setStatus(0);
        order.setTotalMoney(0);
        int result=orderService.add(order);
        if (result>0){
            m.addAttribute("msg","添加成功");
        }else {
            m.addAttribute("msg","添加失败");
        }
        return "order/msg";
    }

    @RequestMapping("/toEdit")
    public String toEdit(String id,Model m){
        Order g= orderService.getOrderById(id);
        m.addAttribute("order",g);
        m.addAttribute("goodss",goodsService.getList(null));

        return "order/edit";
    }

    @RequestMapping(value = "/edit",method= RequestMethod.POST)
    public String edit(Order order,Model m){
        int result=orderService.edit(order);
        if (result>0){
            m.addAttribute("msg","更新成功");
        }else {
            m.addAttribute("msg","更新失败");
        }
        return "order/msg";
    }

    @RequestMapping(value = "/del",method= RequestMethod.GET)
    public String del(String id,Model m){
        int result=orderService.del(id);
        if (result>0){
            m.addAttribute("msg","删除成功");
        }else {
            m.addAttribute("msg","删除失败");
        }
        return "order/msg";
    }

    @RequestMapping(value = "/editTourist",method= RequestMethod.GET)
    public String editTourist(String id,Model m){
        Order g= orderService.getOrderById(id);
        m.addAttribute("order",g);
        return "order/tourist-list";
    }

    @RequestMapping(value = "/getTourist",method= RequestMethod.POST)
    public @ResponseBody
    List<Tourist> getTourist(String orderid, Model m){

        return orderService.getTourist(orderid);
    }

    @RequestMapping(value = "/getTouristOut",method= RequestMethod.POST)
    public @ResponseBody
    List<Tourist> getTouristOut(String orderid, Model m){

        return orderService.getTouristOut(orderid);
    }

    @RequestMapping(value = "/addTourist",method= RequestMethod.POST)
    public @ResponseBody
    String addTourist(String orderid,String tourist_id, Model m){
        int a= orderService.addTourist(orderid,tourist_id);
        if (a>0){
            return "success";
        }else {
            return "fail";
        }
    }

    @RequestMapping(value = "/delTourist",method= RequestMethod.POST)
    public @ResponseBody
    String delTourist(String orderid,String tourist_id, Model m){
        int a= orderService.delTourist(orderid,tourist_id);
        if (a>0){
            return "success";
        }else {
            return "fail";
        }
    }

}
